//  Copyright (c) 2011 Artur Gurgul ( artur.gurgul@gmail.com ). All rights reserved.

#import "DesktopController.h"
#import "DesktopIconView.h"

@implementation DasktopController

@synthesize iconItems;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

#pragma mark - View lifecycle

#define putIcon(tagNum,col,row,textIcon,iconName) desktopIconView = [[DesktopIconView alloc] initWithFrame:CGRectMake(\
col*self.view.frame.size.width/3,\
topSpace + row*self.view.frame.size.width/3,\
self.view.frame.size.width/3, self.view.frame.size.width/3)];\
\
desktopIconView.button.tag = tagNum;\
desktopIconView.textLabel.text = textIcon;\
[desktopIconView.button addTarget:self action:@selector(selectItem:) forControlEvents:UIControlEventTouchUpInside];\
desktopIconView.imageView.image = [UIImage imageNamed:iconName];\
[self.view addSubview:desktopIconView];

// [desktopIconView.button setImage:[UIImage imageNamed:iconName] forState:UIControlStateNormal];\

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
    [super loadView];
    
    DesktopIconView *desktopIconView;
    float topSpace  = 50;
    
    putIcon(1,0,0,@"Ankieta",@"tests.png");
    putIcon(2,0,1,@"Gry",@"game.png");
    putIcon(3,0,2,@"Książka",@"dictionary.png");
    putIcon(4,1,0,@"Baza Danych",@"db.png");
    putIcon(5,1,1,@"Znajomi",@"friends.png");
    putIcon(6,1,2,@"Profil",@"profil.png");
    putIcon(7,2,0,@"Pomoc",@"help.png");
    
    // pobranie danych z cache i nstepanie:
    
    for (id <DesktopItemDelegate> itemItem in iconItems) {
        [itemItem getTitle];
        [itemItem getImage];
    }
}

-(void) selectItem :(UIButton*) sender {
    switch (sender.tag) {
        case 1:{
            TestsController *testsController = [[TestsController alloc] init];
            [self.navigationController pushViewController:testsController animated:YES];
            break;
        }
        default:
            break;
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
